'''Text file manipulation module.

This class allows you to save and load data in a text file.

Notice: This class is not optimized for speed and should not be utilized for
processor-intensive tasks.'''

from os.path import join

class TextFile:
    def __init__(self, file_name, mode = "r"):
        '''A TextFile always takes a file_name as a parameter for the constructor.
        Also, one can also decide the writing permissions for the file (the default
        value is read-only mode).'''

        text_file = open(file_name, mode)

        self.data = text_file.readlines()
        self.data = [l.strip() for l in self.data if len(l.strip()) > 0 and l[0] != '#']

        text_file.close()

    def seek_attribute(self, attribute):
        '''This method returns the line which has the desired attribute, or None,
        in case it was not found. This method stops seeking at the first instance
        of the attribute found.

        The title for the line is stripped.'''
        print("seek_attribute({0})".format(attribute))
        for l in self.data:
            if l.split()[0].lower() == "!"+attribute.lower():
                return " ".join(l.split()[1:])
        return None

    def seek_attribute_list(self, attribute):
        '''This method returns a list of lines which have the desired attribute,
        or an empty list in case the attribute is not found.

        The title for the line is stripped.'''
        #values = [" ".join(l.split()[1:]) for l in self.data if l.split()[0].lower == "!"+attribute.lower()]
        print("seek_attribute_list({0})".format(attribute))

        values = []
        for l in self.data:
            if l.split()[0].lower() == "!"+attribute.lower():
                values.append(" ".join(l.split()[1:]))
        return values

